package bTree.LeetCode;

import javax.swing.tree.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class 二叉搜索树中的插入操作 {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        TreeNode p=root;
        if (p==null){
            return new TreeNode(val);
        }
        while (true){
            int value=p.val;
            if (val>value){
                if (p.right==null){
                    p.right=new TreeNode(val);
                    break;
                }
                p=p.right;

            }else {
                if (p.left==null){
                    p.left=new TreeNode(val);
                    break;
                }
                p=p.left;
            }
        }
        return root;
    }
}